/**
 * @author xxxx
 * @description 保存cookie
 * @param {String} name 需要存储cookie的key
 * @param {String} value 需要存储cookie的value
 * @param {Number} timer 默认存储多少天
 */
function setCookie(name: string,value: string, timer = 1) {
  const days = timer; //默认将被保存 1 天
  const exp  = new Date();
  exp.setTime(exp.getTime() + days*24*60*60*1000);
  document.cookie = name + "="+ escape(value) + ";expires="+ exp.toUTCString();
}

/**
 * @author xxxx
 * @description 获取cookie
 * @param {String} name 需要获取cookie的key
 */
function getCookie(name: string) {
  const arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
  if(arr !== null){
    return unescape(arr[2])
  }
  return null
}

/**
 * @author xxxx
 * @description 删除cookie
 * @param {String} name 需要删除cookie的key
 */
function clearCookie(name: string) {
  const exp = new Date();
  exp.setTime(exp.getTime() - 1);
  const cval = getCookie(name);
  if(cval !== null) {
    document.cookie = name + "=" + cval + ";expires=" + exp.toUTCString();
  }
}
export {setCookie, clearCookie, getCookie}